package cn.tlb.okhttp;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.FormEncodingBuilder;
import com.squareup.okhttp.Headers;
import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.MultipartBuilder;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.RequestBody;
import com.squareup.okhttp.Response;

import java.io.File;
import java.io.IOException;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.tlb.xutilsdownload.R;

/**
 * 1. okhttp 回调方法在子线程中
 */
public class OkhttpMainActivity extends AppCompatActivity {
    @Bind(R.id.content)
    TextView content;
    @Bind(R.id.ImageView)
    ImageView imageView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_okhttp_main);
        ButterKnife.bind(this);
    }

    @OnClick(R.id.button14)
    void ok_get() {
        //创建okHttpClient对象
        OkHttpClient mOkHttpClient = new OkHttpClient();
        //创建一个Request
        final Request request = new Request.Builder()
                .url("https://github.com/square/okhttp/blob/master/README.md")
                .build();
        //new call
        Call call = mOkHttpClient.newCall(request);
        //请求加入调度
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {
            }

            @Override
            public void onResponse(final Response response) throws IOException {
                final String htmlStr = response.body().string();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        content.setText(htmlStr);
                    }
                });
            }
        });
    }

    @OnClick(R.id.button15)
    void ok_post() {
        OkHttpClient okHttpClient = new OkHttpClient();
        FormEncodingBuilder builder = new FormEncodingBuilder();
        builder.add("name", "zhangSan");
        builder.add("age", "20");

        Request request = new Request.Builder()
                //http://localhost:8080/http/upload_response.jsp?name=zhangsan&age=20
                .url("http://192.168.47.146:8080/http/upload_response.jsp")
                .post(builder.build())
                .build();

        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Request request, final IOException e) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        content.setText(e.getMessage());
                    }
                });
            }

            @Override
            public void onResponse(Response response) throws IOException {
                final String text = response.body().string();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        content.setText(text);
                    }
                });
            }
        });
    }

    @OnClick(R.id.button16)
    void upload() {
        File file = new File(Environment.getExternalStorageDirectory(), "/Movies/1.mp4");

        RequestBody fileBody = RequestBody.create(MediaType.parse("application/octet-stream"), file);

        RequestBody requestBody = new MultipartBuilder()
                .type(MultipartBuilder.FORM)
                .addPart(Headers.of("Content-Disposition","form-data; name=\"username\""),
                        RequestBody.create(null, "aaaaaaaa"))
                .addPart(Headers.of("Content-Disposition", "form-data; name=\"mFile\"; filename=\"123.mp4\""),
                        fileBody)
                .build();

        Request request = new Request.Builder()
                .url("http://192.168.47.146:8080/http/servlet/FileUploadServlet")
                .post(requestBody)
                .build();

        Call call = new OkHttpClient().newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Request request, final IOException e) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        content.setText(e.getMessage());
                    }
                });
            }

            @Override
            public void onResponse(Response response) throws IOException {
                final String text = response.body().string();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        content.setText(text);
                    }
                });
            }


        });
    }

    @OnClick(R.id.button17) void sync_loadImage(){
        OkHttpClient okHttpClient = new OkHttpClient();
        Call call = okHttpClient.newCall(new Request.Builder().url("http://p2.qhimg.com/t015a1a31dafb10de31.jpg").build());
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {

            }

            @Override
            public void onResponse(Response response) throws IOException {
                final Bitmap bm = BitmapFactory.decodeStream(response.body().byteStream());
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        imageView.setImageBitmap(bm);
                    }
                });
            }
        });
    }
}
